<template>
  <div class="kd_search">
    <div class="kd_search_left">
      <div class="kd_search_left_top">
        <el-icon><Search /></el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="kd_search_left_bottom">
        <span>账号/姓名:</span>
        <el-input
          placeholder="请输入账号、姓名"
          v-model="paramsData.keyword"
        ></el-input>
      </div>
    </div>
    <div class="kd_search_right">
      <el-button @click="createFn">重置</el-button>
      <el-button type="primary" @click="searchFn">查询搜索</el-button>
    </div>
  </div>
  <div class="kd_add">
    <div class="kd_add_left">
      <el-icon><Tickets /></el-icon>
      <span>数据列表</span>
    </div>
    <div class="kd_add_right">
      <el-button type="primary" @click="dialogFormVisible = true" v-permission="'addUser'">添加{{ dialogFormVisible }}</el-button
      >
    </div>
  </div>
  <el-table :data="listData" border style="min-width: 800px; font-size: 12px"  cell-class-name="align-c">
    <el-table-column prop="id" label="编号" width="50px" />
    <el-table-column prop="username" label="账号" />
    <el-table-column prop="nickName" label="姓名"/>
    <el-table-column prop="email" label="邮箱" />
    <el-table-column prop="createTime" width="170px" label="添加时间">
      <template #default="{ row }">
        {{ formateDate(row.createTime) }}
      </template>
    </el-table-column>
    <el-table-column prop="loginTime" width="170px" label="最后登录">
      <template #default="{ row }">
        {{ formateDate(row.loginTime) }}
      </template>
    </el-table-column>
    <el-table-column label="是否启用">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="onSwitch(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button text type="primary" @click="allocFn(row.id)"
          v-permission="'allocRole'">分配角色</el-button
        >
        <el-button text type="primary" @click="bianJi(row.id)" v-permission="'editRole'">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="kd_pagination">
    <ElConfigProvider :locale="zhCn">
      <el-pagination
        v-model:current-page="paramsData.pageNum"
        v-model:page-size="paramsData.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="searchFn"
        @current-change="searchFn"
      />
    </ElConfigProvider>
  </div>
  <editUserVue
    :dialogFormVisible="dialogFormVisible"
    @hiddenForm="hiddenForm"
    :userId="userId"
  />
  <allocRole :roleShow="roleShow" @roleShowFn="roleShowFn" :id="allocId" />
</template>

<script lang="ts" setup>
import { ref, getCurrentInstance, reactive } from "vue";
import { userListApi, userStatusApi } from "./api";
import { ElConfigProvider, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { formateDate } from "@/utils";
import editUserVue from "./components/editUser.vue";
import allocRole from "./components/allocRole.vue";
const { proxy }: any = getCurrentInstance();

const roleShow = ref(false);
const roleShowFn = () => {
  roleShow.value = false;
  allocId.value=0
};
const allocId = ref(0)
const allocFn = (id:number)=>{
  allocId.value = id
  roleShow.value = true
}

// 表单显隐
const dialogFormVisible = ref(false);
const userId = ref(0);
const bianJi = (id: number) => {
  userId.value = id;
  dialogFormVisible.value = true;
};
const hiddenForm = (value: boolean) => {
  if (value) {
    UserGetApi();
    dialogFormVisible.value = false;
    userId.value=0
  } else {
    dialogFormVisible.value = false;
    userId.value=0
  }
};
const onSwitch = (row: IList) => {
  // console.log(row.id, row.status);
  userStatusApi(row.id as number, row.status)
    .then((res) => {
      if (res.code === 200) {
        console.log(res);
        ElMessage.success("修改成功");
      } else {
        row.status = row.status === 0 ? 1 : 0;
      }
    })
    .catch(() => {
      row.status = row.status === 0 ? 1 : 0;
    });
};

// 请求参数
const paramsData = reactive<IUserParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});
const listData = ref<IList[]>([]);
const total = ref(0);
// 搜索功能
const searchFn = () => {
  UserGetApi();
};
// 重置功能
const createFn = () => {
  paramsData.keyword = "";
  UserGetApi();
};
// 请求Api封装
const UserGetApi = () => {
  userListApi(paramsData).then((res) => {
    if (res.code === 200) {
      total.value = res.data.total;
      listData.value = res.data.list;
    }
  });
};
UserGetApi();

</script>

<style lang="scss" scoped>
:deep(.align-c),:deep(.el-table__header) {
  .cell {
    text-align: center !important;
  }
}
.kd_search {
  display: flex;
  justify-content: space-between;
  box-shadow: 0 0 5px 5px #eee;
  padding: 20px;
  .kd_search_left {
    .kd_search_left_top {
      font-size: 13px;
      display: flex;
      align-items: center;
      span {
        padding-left: 10px;
      }
    }
    .kd_search_left_bottom {
      width: 300px;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 10px;
      span {
        font-size: 13px;
        padding-right: 10px;
      }
      :deep(.el-input) {
        width: 158px;
        height: 28px;
      }
    }
  }
  .kd_search_right {
  }
}
.kd_add {
  display: flex;
  justify-content: space-between;
  box-shadow: 0 0 5px 5px #eee;
  padding: 20px;
  margin: 22px 0;
  .kd_add_left {
    display: flex;
    align-items: center;
    span {
      padding-left: 10px;
    }
  }
  .kd_add_right {
  }
}
.kd_pagination {
  display: flex;
  flex-direction: row-reverse;
  margin-top: 20px;
}
</style>
